package yl.hs.bmipfull.service.equipment.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.equipment.IInspectionRegionAdministrationDao;
import yl.hs.bmipfull.pojo.equipment.DeviceInspectionEntity;
import yl.hs.bmipfull.pojo.equipment.InspectionRegionAdministrationEntity;
import yl.hs.bmipfull.service.equipment.IInspectionRegionAdministrationService;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;


import java.util.List;
import java.util.Map;

/**
 * @author 韩纪旺
 * @apiNote 巡检区域信息服务层接口实现
 * @since 2022年11月25日 17时00分26秒 <br/>
 */
@Service
public class InspectionRegionAdministrationServiceImpl extends EquipmentBaseService implements IInspectionRegionAdministrationService {

    @Autowired
    IInspectionRegionAdministrationDao dao;

    @Override
    public List<InspectionRegionAdministrationEntity> queryList(PageQuery pageQuery) throws Exception {
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),
                InspectionRegionAdministrationEntity.class);
        pageQuery.checkSortSides(InspectionRegionAdministrationEntity.class);
        return dao.queryPage(pageQuery.getQuery(),filters);
    }

    @Override
    public PageResult<InspectionRegionAdministrationEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(InspectionRegionAdministrationEntity.class);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),
                InspectionRegionAdministrationEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        pageQuery.checkSortSides(InspectionRegionAdministrationEntity.class);
        pageQuery.getQuery().put("sortSide", String.join(",",pageQuery.getSortSides()));
        PageResult<InspectionRegionAdministrationEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public InspectionRegionAdministrationEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveInspectionRegionAdministration(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        InspectionRegionAdministrationEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new InspectionRegionAdministrationEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(InspectionRegionAdministrationEntity.class));
            result = dao.insertNew(entity);
                writeLog(ActionType.CREATE,entity.getSn(),"",null,entity.getClass());
                writeLog(ActionType.CREATE, entity.getSn(), "插入巡检区域信息信息", Tl.toJson(entity), InspectionRegionAdministrationEntity.class);
        } else {
            //执行修改
            entity = findBySn(sn);
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            var backJson = entity.toJson();
                writeLog(ActionType.CREATE,entity.getSn(),changeLog,backJson,entity.getClass());
                writeLog(ActionType.UPDATE, sn, "更新巡检区域信息信息", Tl.toJson(entity), InspectionRegionAdministrationEntity.class);
        }
        if (result > 0) {
            var user = HsApp.getCurrent();
            saveGS(
                    entity.getSn(),
                    entity.getInspectionName(),
                    String.join(",",
                            entity.getInspectionName(),
                            entity.getRemark()

                    )    ,
                    entity,
                    HsApp.getClient()!=null ? HsApp.getCurrent().getUserSN():null,
                    user.getOrganizationCode()
            );
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] snArray) {
        Integer result = dao.changeStateBySN(snArray, DataState.DELETED.code);
        if (result > 0) {
            for (String sn : snArray) {
                writeLog(ActionType.DELETE, sn, "","",InspectionRegionAdministrationEntity.class);

            }
        }
        return result;
    }
}